Popular recipes tagged "meta:loc<10"http://code.activestate.com/recipes/tags/meta:loc%3C10/2017-07-17T05:53:45-07:00ActiveState Code RecipesInserting Images on PDF Pages (Python) 2017-05-17T21:10:26-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580803-inserting-images-on-pdf-pages/ <p style="color: grey"> Python recipe 580803 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>). </p> <p>Version 1.11.0 of PyMuPDF allows putting an image on an existing PDF page. The following example puts the same image on every page of a given PDF - like a thumbnail.</p> Shoelace Formula for polygonal area (Python) 2017-07-17T05:53:45-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/580812-shoelace-formula-for-polygonal-area/ <p style="color: grey"> Python recipe 580812 by <a href="/recipes/users/398009/">Paddy McCarthy</a> (<a href="/recipes/tags/2d/">2d</a>, <a href="/recipes/tags/area/">area</a>). </p> <p>Copied, by author from "Paddy3118 Go deh!: Python investigation of the Shoelace Formula for polygonal area <a href="http://paddy3118.blogspot.com/2017/07/python-investigation-of-shoelace.html#ixzz4n43Dqhaa" rel="nofollow">http://paddy3118.blogspot.com/2017/07/python-investigation-of-shoelace.html#ixzz4n43Dqhaa</a> " Where there is more meat on the bone (under a different license though).</p> groupby() For Unsorted Input (Python) 2017-05-12T10:40:58-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580800-groupby-for-unsorted-input/ <p style="color: grey"> Python recipe 580800 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/grouping/">grouping</a>, <a href="/recipes/tags/lazy/">lazy</a>). </p> <p>We all know the <code>groupby()</code> which is available in the <code>itertools</code> standard module. This one yields groups of consecutive elements in the input which are meant to be together in one group. For non-consecutive elements this will yield more than one group for the same key.</p> <p>So effectively, <code>groupby()</code> only reformats a flat list into bunches of elements from that list without reordering anything. In practice this means that for input sorted by key this works perfect, but for unsorted input it might yield several groups for the same key (with groups for other keys in between). Typically needed, though, is a grouping with reordering if necessary.</p> <p>I implemented a likewise lazy function (yielding generators) which also accepts ungrouped input.</p> Rotate a PDF page in 3 lines (Python) 2016-11-06T11:33:59-08:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580713-rotate-a-pdf-page-in-3-lines/ <p style="color: grey"> Python recipe 580713 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>). Revision 2. </p> <p>PyMuPDF v1.9.3 now supports several new features for manipulating PDFs.</p> <p>Here is an example to rotate a page with just a few lines of Python code.</p> Merge unique items from multiple lists into a new list (Python) 2016-04-05T18:11:49-07:00Johannes Shttp://code.activestate.com/recipes/users/4193888/http://code.activestate.com/recipes/580634-merge-unique-items-from-multiple-lists-into-a-new-/ <p style="color: grey"> Python recipe 580634 by <a href="/recipes/users/4193888/">Johannes S</a> (<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/merge/">merge</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/set/">set</a>). Revision 2. </p> <p>Suppose you have multiple lists. You want to print all unique items from the list. Now, what you could do is merge the lists into one_big_list (e.g., a + b +c), and then iterate over each item in one_big_list, etc. The solution proposed here gets this done faster and in one line of code. How? By using <strong>a python set</strong>. A python set is a dictionary that contains only keys (and no values). And dictionary keys are, by definition, unique. Hence, duplicate items are weeded out automatically. Once you have the set, you can easily convert it back into a list. As easy as that!</p> tic (Python) 2016-07-29T01:52:11-07:00YasGonhttp://code.activestate.com/recipes/users/4194509/http://code.activestate.com/recipes/580693-tic/ <p style="color: grey"> Python recipe 580693 by <a href="/recipes/users/4194509/">YasGon</a> . Revision 2. </p> <p>toe</p> Python one-liner to compare two files (Python) 2016-03-28T21:36:31-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580632-python-one-liner-to-compare-two-files/ <p style="color: grey"> Python recipe 580632 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>As the title says ...</p> <p>It prints True if the files compared are the same, and False if they differ (either in size or in content).</p> Generate a set of random integers (Python) 2016-02-18T19:34:17-08:00Lance Spencehttp://code.activestate.com/recipes/users/4193647/http://code.activestate.com/recipes/580613-generate-a-set-of-random-integers/ <p style="color: grey"> Python recipe 580613 by <a href="/recipes/users/4193647/">Lance Spence</a> (<a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>This is just a short and simple script that generates a set of random numbers. You enter how many random numbers you want and the program will generate them.</p> <p>The num is multiplied by 10 in the upper limit within the for loop to give you larger generated integers. Adjust it as needed.</p> Set the logging level to every logger (Python) 2015-10-28T23:16:26-07:00Tim McNamarahttp://code.activestate.com/recipes/users/4193044/http://code.activestate.com/recipes/579119-set-the-logging-level-to-every-logger/ <p style="color: grey"> Python recipe 579119 by <a href="/recipes/users/4193044/">Tim McNamara</a> (<a href="/recipes/tags/configuration/">configuration</a>, <a href="/recipes/tags/logging/">logging</a>). </p> <p>Python's logging allocates a name to every logger. That makes it hard to do something like, setting everything to <code>logging.ERROR</code>. Here's one way you might go about that:</p> Add function to Python's __builtin__ module through C API - makefile (Text) 2015-10-16T12:11:36-07:00airweenhttp://code.activestate.com/recipes/users/4192997/http://code.activestate.com/recipes/579111-add-function-to-pythons-__builtin__-module-through/ <p style="color: grey"> Text recipe 579111 by <a href="/recipes/users/4192997/">airween</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/makefile/">makefile</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Makefile for add function to __builtin__ module through C API.</p> <p>C source is here:</p> <p><a href="https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/" rel="nofollow">https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/</a></p> <p>Python script is here:</p> <p><a href="https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579112-add-function-to-__builtin__-module-through-c-api-c/</a></p> Hivemind (Python) 2015-07-15T12:37:13-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579082-hivemind/ <p style="color: grey"> Python recipe 579082 by <a href="/recipes/users/4192487/">Oscar Byrne</a> (<a href="/recipes/tags/borg/">borg</a>, <a href="/recipes/tags/defaultdict/">defaultdict</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/hashing/">hashing</a>, <a href="/recipes/tags/hivemind/">hivemind</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/singleton/">singleton</a>, <a href="/recipes/tags/state/">state</a>). </p> <p>Inspired by the ever-popular Borg pattern, objects inheriting from Hivemind share state if initialised with the same arguments</p> Drop a minimal, valid Windows executable file to disk, for testing (Python) 2015-03-03T10:47:32-08:00Simon Harrisonhttp://code.activestate.com/recipes/users/4191738/http://code.activestate.com/recipes/579029-drop-a-minimal-valid-windows-executable-file-to-di/ <p style="color: grey"> Python recipe 579029 by <a href="/recipes/users/4191738/">Simon Harrison</a> (<a href="/recipes/tags/executable/">executable</a>, <a href="/recipes/tags/pe/">pe</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 3. </p> <p>Sometimes I need to create a valid windows executable file from a Python script for the sake of running a test.</p> hollow (Perl) 2015-03-08T22:16:06-07:00Jyh Chonghttp://code.activestate.com/recipes/users/4191780/http://code.activestate.com/recipes/579033-hollow/ <p style="color: grey"> Perl recipe 579033 by <a href="/recipes/users/4191780/">Jyh Chong</a> . </p> <h4 id="usrbinperl-w">!/usr/bin/perl -w</h4> <pre class="prettyprint"><code> print "Content-type: text/html\n\n"; print "Hello, world"; </code></pre> smart copy (Python) 2015-02-06T09:45:12-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579020-smart-copy/ <p style="color: grey"> Python recipe 579020 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/shutil/">shutil</a>). </p> <p>take a glob expression, a source directory and a destination directory to copy each files matching the glob in the appropriate directory</p> <pre class="prettyprint"><code>glob = */*.txt src_dir = ./a/b dst_dir = /z/x/y </code></pre> <p>if the glob match a file <code>./a/b/c/foo.txt</code>, it will copy it in <code>/z/x/y/c/foo.txt</code> (and create the missing directory if needed)</p> <p>Require Python3.4, code tab indented</p> Get external IP & geolocation in bash. (Bash) 2014-11-30T00:46:28-08:00manuhttp://code.activestate.com/recipes/users/4191225/http://code.activestate.com/recipes/578972-get-external-ip-geolocation-in-bash/ <p style="color: grey"> Bash recipe 578972 by <a href="/recipes/users/4191225/">manu</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/geocoding/">geocoding</a>, <a href="/recipes/tags/geolocation/">geolocation</a>). Revision 2. </p> <p>Very simple way to get external IP and geolocation uysing dig and geoiplookup.</p> <p><code>dig</code> is cool to obtain my external IP and I use <code>geoiplookup</code> to convert IP to location. You need geoip-bin and, geoip-database (and/or geoip-database-contrib and geoip-database-extra). In Debian, database seems update monthly.</p> <p>It's just a tip.</p> Proxying WCF service (XML) 2014-11-07T13:03:18-08:00Tomáš Rampashttp://code.activestate.com/recipes/users/4179816/http://code.activestate.com/recipes/578958-proxying-wcf-service/ <p style="color: grey"> XML recipe 578958 by <a href="/recipes/users/4179816/">Tomáš Rampas</a> (<a href="/recipes/tags/dotnet/">dotnet</a>, <a href="/recipes/tags/fiddler/">fiddler</a>, <a href="/recipes/tags/wcf/">wcf</a>). </p> <p>Sometimes capturing WCF service communication e.g. with Fiddler is necessary. Following is the part of WCF service application config proxying WCF to localhost:8888</p> Random Password Generation (Python) 2014-08-10T15:50:40-07:00Paul Wolfhttp://code.activestate.com/recipes/users/4190553/http://code.activestate.com/recipes/578920-random-password-generation/ <p style="color: grey"> Python recipe 578920 by <a href="/recipes/users/4190553/">Paul Wolf</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/strong/">strong</a>). </p> <p>Generate a password (or other secure token) using a pattern language similar to regular expressions. We'll use the <code>strgen</code> module that enables a user to generate test data, unique ids, passwords, vouchers or other randomized data very quickly using a template language. The template language is superficially similar to regular expressions but instead of defining how to match or capture strings, it defines how to generate randomized strings.</p> Greatest common divisor (Python) 2014-09-19T08:03:02-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578937-greatest-common-divisor/ <p style="color: grey"> Python recipe 578937 by <a href="/recipes/users/4190606/">juan</a> . </p> <p>Calculate the Greatest common divisor from two numbers</p> list all locale (Python) 2014-04-19T13:41:38-07:00Shane Wanghttp://code.activestate.com/recipes/users/4171830/http://code.activestate.com/recipes/578863-list-all-locale/ <p style="color: grey"> Python recipe 578863 by <a href="/recipes/users/4171830/">Shane Wang</a> . </p> <p>list all locales via Python</p> create a unique session key (Python) 2014-07-03T16:32:53-07:00john stinsonhttp://code.activestate.com/recipes/users/4190325/http://code.activestate.com/recipes/578903-create-a-unique-session-key/ <p style="color: grey"> Python recipe 578903 by <a href="/recipes/users/4190325/">john stinson</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/sessions/">sessions</a>). </p> <p>A simple function that will generate a secure and unique session key.</p>